package com.zmj.sy.mom.srv.aps.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.WorkDetailTask;
import com.zmj.sy.mom.srv.aps.mapper.WorkDetailTaskMapper;
import org.springframework.stereotype.Component;

@Component
public class MkPartUtils {
    
    private final WorkDetailTaskMapper workDetailTaskMapper;

    public MkPartUtils(WorkDetailTaskMapper workDetailTaskMapper) {
        this.workDetailTaskMapper = workDetailTaskMapper;
    }

    /**
     * 是否支持库检工序,
     * 父级PLMID包含：P1、PDB、P2、P3、P4、P5、P6、-0103、-0102、-0202、-0203 或者 父级组件名称中包含：拼、左侧护板、右侧护板、挡板组件
     * @param plmId
     * @param parentMaterName 父级组件名称
     * @return
     */
    public static boolean supportKjPhase(String plmId, String parentMaterName) {
        return plmId.matches(".*(P[1-6]|PDB|-0[123]0[23]).*") || parentMaterName.matches(".*(拼|[左右]侧护板|挡板组件).*");
        // 优化成正则，由于正则表达式效率低，原始方法保留
        /*
        if (plmId.contains("P1") || plmId.contains("PDB") || plmId.contains("P2") || plmId.contains("P3") || plmId.contains("P4") || plmId.contains("P5") || plmId.contains("P6")
            || plmId.contains("-0103") || plmId.contains("-0102") || plmId.contains("-0202") || plmId.contains("-0203")
            || parentMaterName.contains("拼") || parentMaterName.contains("左侧护板") || parentMaterName.contains("右侧护板") || parentMaterName.contains("挡板组件")) {
            return true;
        }
        return false;

         */
    }

    private static String getAssembleType(String plmId, String materName){
        if (materName.equals("挡板组件") && plmId.contains("A_")) {
            return "DBA";
        }
        if (materName.equals("挡板组件") && plmId.endsWith("A")) {
            return "DBA";
        }
        if (materName.contains("底座一拼") || plmId.contains("-0301P")) {
            return "D";
        }
        if (materName.contains("顶梁一拼") || plmId.contains("-0101P")) {
            return "L";
        }
        if (materName.contains("掩护梁一拼") || plmId.contains("-0201P")) {
            return "Y";
        }
        if(materName.contains("底座体") || plmId.contains("-0301B")){
            return "DT";
        }
        if (materName.contains("顶梁左侧护板")) {
            return "DZ";
        }
        if (materName.contains("顶梁右侧护板")) {
            return "DY";
        }
        if (materName.contains("掩护梁左侧护板")) {
            return "YZ";
        }
        if (materName.contains("掩护梁右侧护板")) {
            return "YY";
        }
        if (materName.contains("挡板组件")) {
            return "DB";
        }
        throw SyExceptionUtils.e("没有找到该数据的类型!");
    }

    /**
     * 获取唯一号
     */
    public String getUniqueCode(String constructNo, String plmId, String materName) {
        QueryWrapper<WorkDetailTask> wrapper = Wrappers.query();
        wrapper.eq("awd.mater_no", plmId);
        wrapper.eq("o.construct_no", constructNo);
        wrapper.ne("awd.unique_code", "PD");
        String s = workDetailTaskMapper.selectMaxCode(wrapper);

        int max = 0;
        if(s != null){
            String substring = s.substring(s.lastIndexOf("-")+1);
            max = Integer.parseInt(substring);
        }

        return getAssembleType( plmId, materName) + constructNo + "-" + String.format("%03d",max + 1);
    }

}
